<?php
if(isset($_REQUEST['act']) && $_REQUEST['act']=='lists'){
	include_once '/var/www/html/new/com.inc.php';
	include_once '/var/www/html/new/d/class/abc/PFTCoreAPI.class.php';
	if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆过期","code":0}');
	$memberSID = $memberID = $_SESSION['sid'];

	$pid = $_REQUEST['pid'] + 0;// 产品PID
	$aid = $_REQUEST['sid'] + 0;// 产品上级供应商ID
	if($pid==0) exit('{"status":"fail","msg":"参数错误","code":0}');

	$pageSize    = $_REQUEST['pageSize'] ? $_REQUEST["pageSize"]:15;
	$currentPage = $_REQUEST["currentPage"] ? $_REQUEST["currentPage"]:1;
	$startPos    = ($currentPage-1)*$pageSize;

	$uupaid = $aid ? $memberSID:0;

	$ac = $GLOBALS['_ac'];
	$pw = $GLOBALS['_pw'];
	$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

	// 获取会员自己的结算价
	$pStorage = PFTCoreAPI::pStorage($s,$_SESSION['saccount'], $pid, $aid, date('Y-m-d'), 0);
	$priSelf  = $pStorage['js']['p'];
	if($priSelf==-1) exit('{"status":"fail","msg":"无权分销商该产品"}');


	// 获取分销商
	$condition = ''; $distributors = array();
	$_REQUEST['txt'] = addslashes($_REQUEST['txt']);
	// if($_REQUEST['txt']) $condition = " and m.dname like '%".$_REQUEST['txt']."%' or m.account like '%".$_REQUEST['txt']."%'";
	if($_REQUEST['txt']) $condition = " and (instr(m.dname,'".$_REQUEST['txt']."') or m.account='".$_REQUEST['txt']."')";
	$sql = "select m.id,m.dname,m.account from pft_member_relationship r left join pft_member m on r.son_id=m.id where r.parent_id=$memberID and r.ship_type=0 and r.status=0 and son_id<>$memberID and m.status<3 and m.id>1 and length(m.account)<11 $condition limit $startPos,$pageSize";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $distributors[$row['id']] = $row;

	// 统计分销商
	$sql = "select count(m.id) as c from pft_member_relationship r left join pft_member m on r.son_id=m.id where r.parent_id=$memberID and r.ship_type=0 and r.status=0 and son_id<>$memberID and m.status<3 and m.id>1 and length(m.account)<11 $condition";
	$GLOBALS['le']->query($sql);
	$GLOBALS['le']->fetch_assoc();
	$totalRow = $GLOBALS['le']->f('c');
	// if($_SESSION['sid']==1433) echo $sql;
	$totalPge = ceil($totalRow / $pageSize);


	$dist_list_a = array_keys($distributors);// 分销商ID数组
	$dist_list_s = implode(',', $dist_list_a);
	$dist_list_l = count($dist_list_a);
	if($dist_list_l==0) exit('{"status":"success","lists":"[]"}');

	// 获取原配置分销差价
	$sql = "select dprice,pid,id from uu_priceset where aid=$uupaid and tid=$pid and pid in ($dist_list_s)";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $dprice_a[$row['pid']] = $row;


	$response_list = array();
	if($uupaid==0){// 直销配置
		
		// 获取记录  pft_product_sale_list
		$sql = "select * from pft_product_sale_list where aid=$memberSID and fid in ($dist_list_s) limit $dist_list_l";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $direct[$row['fid']] = $row;
		foreach($dist_list_a as $dist_mid){
			$distributors[$dist_mid]['js'] = -1;
			$pids_a = explode(',', $direct[$dist_mid]['pids']);
			if($pids_a[0]=='') continue;// 没权限直接不显示价格
			if($pids_a[0]=='A'){
				$distributors[$dist_mid]['js'] = $dprice_a[$dist_mid]['dprice'] / 100 + $priSelf;
				continue;
			}
			if(!in_array($pid, $pids_a)) continue;
			$distributors[$dist_mid]['js'] = $dprice_a[$dist_mid]['dprice'] / 100 + $priSelf;
		}
				
	}else{
		
		// 获取已设置的记录  pft_p_apply_evolute
		$evolute = array();
		$sql = "select id,aids,lvl,status,fid from `pft_p_apply_evolute` where sid=$memberSID and pid=$pid and fid in ($dist_list_s) limit $dist_list_l";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $evolute[$row['fid']] = $row;
		foreach($dist_list_a as $dist_mid){
			$distributors[$dist_mid]['js'] = -1;
			if(!isset($evolute[$dist_mid])) continue;
			if($evolute[$dist_mid]['status']==1) continue;
			$distributors[$dist_mid]['js'] = $dprice_a[$dist_mid]['dprice'] / 100 + $priSelf;
		}
		
	}

	$response = array(
		'list'       => $distributors,
		'last'       => ($currentPage==$totalPge) ? 0:$currentPage+1,
		'status'     => 'success',
		'totalPage'  => $totalPge,
	);
	exit(json_encode($response));
}


include('/var/www/html/new/d/common/header.html');
include('/var/www/html/new/d/common/left.html');
include('/var/www/html/new/d/class/abc/PFTCoreAPI.class.php');

$pid = $_REQUEST['pid'] + 0;

// 获取会员自己的结算价
$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$uupaid   = $_REQUEST['sid'] ? $_REQUEST['sid']:$_SESSION['sid'];
$pStorage = PFTCoreAPI::pStorage($s, $_SESSION['saccount'], $pid, $uupaid, date('Y-m-d'), 0);
$priSelf  = $pStorage['js']['p'];
if($priSelf==-1) exit('无权分销商该产品');


// 获取产品信息
$sql = "select l.title as ltitle,t.title as ttitle from uu_products p left join uu_land l on l.id=p.contact_id left join uu_jq_ticket t on t.pid=p.id where p.id=$pid limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
$ttitle = $GLOBALS['le']->f('ttitle');
$ltitle = $GLOBALS['le']->f('ltitle');



//include('/var/www/html/new/d/tpl/new_tpl/ad_config_page/ad_config.tpl');
include('/var/www/html/new/d/tpl/new_tpl/ad_config_d/ad_config.tpl');
include('/var/www/html/new/d/common/footer.html');





// print_r($distributors);
// pid=7104&sid=3385&lvl=1&aids=3385